﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using ihongma.Job;
using log4net;
using Quartz;
using System.Configuration;
using Quartz.Impl;
using ihongma.Web.UI;
namespace ihongma.Web
{
     
    public class Global : System.Web.HttpApplication
    {

        private static ILog logger = LogManager.GetLogger(typeof(Global));
        private IScheduler sched;
        protected void Application_Start(object sender, EventArgs e)
        {
            //控制台就放在Main
          

                log4net.Config.XmlConfigurator.Configure();
            logger.Debug("Application_Start");
            
            //从配置中读取任务启动时间
            int indexStartHour = Convert.ToInt32(ConfigurationManager.AppSettings["IndexStartHour"]);
            int indexStartMin = Convert.ToInt32(ConfigurationManager.AppSettings["IndexStartMin"]);
            ISchedulerFactory sf = new StdSchedulerFactory();
            sched = sf.GetScheduler();
            JobDetail job = new JobDetail("job1", "group1", typeof(job));//IndexJob为实现了IJob接口的类
            Trigger trigger = TriggerUtils.MakeDailyTrigger("tigger1", indexStartHour, indexStartMin);//每天10点3分执行
            trigger.JobName = "job1";
            trigger.JobGroup = "group1";
            trigger.Group = "group1";
            
            //logger.Info(trigger.FinalFireTimeUtc.Value.ToLocalTime());
            sched.AddJob(job, true);
            var dd=sched.ScheduleJob(trigger);
            logger.Info(dd.ToString());
            //IIS启动了就不会来了
            sched.Start();
            //启动索引库的扫描线程（生产者）
          SearchHelper.GetInstance().CustomerStart();
            
            
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
           //logger.Error("网络出现未处理异常:", HttpContext.Current.Server.GetLastError());
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {
            logger.Debug("Application_End");
            if (sched != null)
            {
                sched.Shutdown(true);
            }
        }
    }
}